package com.OA.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.OA.entity.BulletinNoticeInformation;
import com.OA.entity.Menu_clz;
import com.OA.entity.Role_clz;
import com.OA.entity.User;
import com.OA.service.BulletionService_clz;
import com.OA.util.Page;
import com.alibaba.fastjson.JSON;
/**
 * 公告管理的公告页面的控制层类
 * @author clz
 *
 */
@Controller
@RequestMapping("bulletin")
public class BulletinController_clz {

	//公告管理抽象类
	@Autowired
	BulletionService_clz bulletionService_clz;
	
	/**
	 * 增加或修改数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="updataOrSaveBulletion.do")
	public String updataOrSaveBulletion(BulletinNoticeInformation bulletinNoticeInformation) {
		int bn_id = bulletinNoticeInformation.getBn_id();
		if (bn_id == -1) {
			bulletionService_clz.insertBulletinNoticeInformation(bulletinNoticeInformation);
		}else {
			bulletionService_clz.updateBulletinNoticeInformation(bulletinNoticeInformation);
		}
		return "ok";
	}
	
	/**
	 * 删除数据
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="remove.do")
	public String remove(@RequestParam(value="bu_ids[]") Integer[] bu_ids) throws Exception{
		for (int i = 0; i < bu_ids.length; i++) {
			System.out.println(bu_ids[i]);
			bulletionService_clz.remove(bu_ids[i]);
		}
		return "ok";
		
	}
	
	/**
	 * 删除节点
	 * @param menuidDelete
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="removeMenu.do")
	public String removeMenu(int menuidDelete,int menuparentldDelete) {
		//删除角色菜单关系表
		bulletionService_clz.removeRoleMenu(menuidDelete);
		//删除菜单
		bulletionService_clz.removeMenu(menuidDelete);
		if (menuparentldDelete == 1) {
			//获取所有的子节点
			List<Menu_clz> list = bulletionService_clz.MenuByMenuparentld(menuidDelete);
			for(Menu_clz m : list) {
				//删除角色菜单关系表
				bulletionService_clz.removeRoleMenu(m.getMenuid());
				//删除菜单
				bulletionService_clz.removeMenu(m.getMenuid());
			}
		}
		
		return "ok";
	}
	
	/**
	 * 修改菜单
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="updataMenu.do")
	public String updataMenu(Menu_clz menu) {
		bulletionService_clz.updataMenu(menu);
		return "ok";
	}
	
	
	/**
	 * 通过菜单的id查看菜单 回填数据
	 */
	@ResponseBody
	@RequestMapping(value="menuBymenuid.do")
	public Menu_clz menuBymenuid(int menuid) {
		System.out.println("表单回填");
		Menu_clz menu = bulletionService_clz.menuByMenuId(menuid);
		return menu;
	}
	/**
	 * 节点的插入
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="saveOrUpdate.do")
	public String saveOrUpdate(Menu_clz menu) {
		if (menu.getMenuid() == -1) {
			if (menu.getMenuUrl() == "") {
				menu.setMenuUrl(null);
			}
			System.out.println(menu.getMenuparentld());
			//插入菜单
			bulletionService_clz.saveMenu(menu);
			//获取新的菜单id
			int menuid = bulletionService_clz.menuIdByMenuName(menu.getMenuname());
			//查询角色表名单
			List<Role_clz> roleList = bulletionService_clz.selectAllRole();
			//循环插入关系表
			for (Role_clz r : roleList) {
				System.out.println(r.getRolename());
				bulletionService_clz.insectRolemenu(r.getRoleid(), menuid);
			}
			
		}
		
		return "ok";
	}
	
	
	
	/**
	 * 通过userId查询数据形成树形菜单
	 * @param userId
	 * @param req
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="bulletinTreeMenuByUserId.do")
	public String bulletinTreeMenuByUserId(int userId,int parentId,HttpServletRequest req) throws UnsupportedEncodingException {
		List<Menu_clz> list = bulletionService_clz.bulletinTreeMenuByUserId(userId);
		List<Menu_clz> listReturn = new ArrayList<Menu_clz>();
		for(Menu_clz m : list) {
			if (m.getMenuLevel()!=2|| m.getMenuparentld() == parentId) {
				listReturn.add(m);
				System.out.println(m.getMenuname()+"----"+m.getMenuLevel());
			}
		}
		User user = new User();
		user.setUserId(userId);
		req.getSession().setAttribute("menu", listReturn);
		req.getSession().setAttribute("user", user);
		return "bulletinM";
	}
	
	/**
	 * 通过userId查询数据形成树形菜单
	 * @param userId
	 * @param req
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@ResponseBody
	@RequestMapping(value="bulletinMenuByUserId.do")
	public List<Menu_clz>  bulletinMenuByUserId(int userId,int parentId,HttpServletRequest req) throws UnsupportedEncodingException {
		List<Menu_clz> list = bulletionService_clz.bulletinTreeMenuByUserId(userId);
		List<Menu_clz> listReturn = new ArrayList<Menu_clz>();
		for(Menu_clz m : list) {
			if (m.getMenuLevel()!=2|| m.getMenuparentld() == parentId) {
				listReturn.add(m);
				System.out.println(m.getMenuname()+"----"+m.getMenuLevel());
			}
		}
		User user = new User();
		user.setUserId(userId);
		req.getSession().setAttribute("user", user);
		return listReturn;
	}
	
	
	/**
	 * 跳转到公告页面
	 * @param title
	 * @return
	 */
	@RequestMapping(value="bulletinContent.do")
	public String bulletinContent(int bn_id,HttpServletRequest req) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//表格展示的日期类型格式
		System.out.println(bn_id);
		//查询公告页面的内容
		BulletinNoticeInformation bulletinNoticeInformation = bulletionService_clz.bulletionContent(bn_id);
		//转换时间
		String dataStr = simpleDateFormat.format(bulletinNoticeInformation.getReleaseTime());
		bulletinNoticeInformation.setTime(dataStr);
		//获取发件人名字
		String publisherName = bulletionService_clz.userNameById(bulletinNoticeInformation.getPublisherId());
		bulletinNoticeInformation.setPublisherName(publisherName);
		req.getSession().setAttribute("bulletinNoticeInformation", bulletinNoticeInformation);
		System.out.println(bulletinNoticeInformation.getContent());
		return "bulletin/bulletinContent";
	}
	/**
	 * 跳转到公告内容增加修改页面
	 * @param title
	 * @return
	 */
	@RequestMapping(value="bulletinContentUpdate.do")
	public String bulletinContentUpdate(int bn_id,String category,HttpServletRequest req) {
		System.out.println(bn_id);
		BulletinNoticeInformation bulletinNoticeInformation = new BulletinNoticeInformation();
		if (bn_id != -1) {
			//查询公告页面的内容
			bulletinNoticeInformation = bulletionService_clz.bulletionContent(bn_id);
			System.out.println(bulletinNoticeInformation.getTitle());
		}
		bulletinNoticeInformation.setCategory(category);
		bulletinNoticeInformation.setBn_id(bn_id);
		req.getSession().setAttribute("bulletinContentUpdate", bulletinNoticeInformation);
		return "bulletin/updataOrSaveBulletin";
	}
	
	
	/**
	 * 公告管理普通员工的分页查询
	 * @param page
	 * @param category
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping(value="selectBulletinOrNotice.do")
	public void selectBulletinOrNotice(Page<BulletinNoticeInformation> page,String category,String firstData,String nextData, String title,HttpServletResponse resp) throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//表格展示的日期类型格式
		SimpleDateFormat simpleDateFormatSelect = new SimpleDateFormat("yyyy-MM-dd");//搜索传入的字符串日期类型格式
		Date firstD = null;
		Date nextD = null;
		System.out.println(category+"飒飒飒飒");
		//日期转成Date类型
		if (firstData != null && firstData != "") {
			firstD =  simpleDateFormatSelect.parse(firstData);
			System.out.println(page.getPageNo()+"---"+page.getPageSize()+"---"+firstD);
		}
		//日期转成Date类型
		if (nextData != null && nextData != "") {
			nextD =  simpleDateFormatSelect.parse(nextData);
		}
		
		//分页查询
		Page<BulletinNoticeInformation> pageReturn = bulletionService_clz.selectBulletionByCategory(page, category,firstD,nextD,title);
		resp.setCharacterEncoding("utf-8");
		Date date = null;
		String dateStr = null;
		//日期类型转换成Date类型
		for (int i = 0; i < pageReturn.getRows().size(); i++) {
			date = pageReturn.getRows().get(i).getReleaseTime();
			dateStr = simpleDateFormat.format(date);
			pageReturn.getRows().get(i).setTime(dateStr);
		}
		System.out.println(page.getRows().size()+":数量");
		String json = JSON.toJSONString(pageReturn);
		try {
			resp.getWriter().write(json);
			resp.getWriter().flush();
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
